package com.csusm.geo.science.tutor;

import java.util.ArrayList;
import java.util.HashMap;
import android.app.ListActivity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class DragDropActivity extends ListActivity {

	@SuppressWarnings("rawtypes")
	ArrayList allContents = null;
	ArrayList<String> rcontent = null;
	ArrayList<String> lcontent = null;
	ArrayList<Integer> images;
	HashMap<String, String> answers = null;
	Match match = new Match();

	// Called when submit button is clicked
	public void onClickSubmit(View v) {
		// check if the item match and update the image
		for (int i = 0; i < lcontent.size(); i++) {
			String temp = answers.get(lcontent.get(i));
			if (temp.equalsIgnoreCase(rcontent.get(i))) {
				images.add(i, R.drawable.tick2);
			} else {
				images.add(i, R.drawable.wrong);
				Toast.makeText(getApplicationContext(),
						"You are close.Try again", Toast.LENGTH_SHORT).show();
			}
		}
		setListAdapter(new DragDropAdapter(this, new int[] { R.layout.items },
				new int[] { R.id.TextView01 }, allContents));
	}

	// Called when the activity is first created. 
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.dragdroplistview);
		
		//get the passed values from previous activity
		Intent i = getIntent();
		String selLoc = (String) i.getSerializableExtra("SelLoc");
		// get the matchf table appropriate values
		ActivityDbHelper activityDbHelper = new ActivityDbHelper();
		match = activityDbHelper.getLinks(selLoc);
		Toast.makeText(getApplicationContext(),
				"Match definitions with appropriate left items",
				Toast.LENGTH_SHORT).show();

		// populate data into arraylist
		populateData();
		// Set the data to list adapter
		setListAdapter(new DragDropAdapter(this, new int[] { R.layout.items },
				new int[] { R.id.TextView01 }, allContents));
		ListView listView = getListView();

		if (listView instanceof DragDropListview) {
			((DragDropListview) listView).setDropListener(mDropListener);
			((DragDropListview) listView).setRemoveListener(mRemoveListener);
			((DragDropListview) listView).setDragListener(mDragListener);
		}
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	private void populateData() {
		String tempb = match.getMatchb();
		tempb = tempb.trim();
		String[] mListOptions = tempb.split(";");
		rcontent = new ArrayList<String>(mListOptions.length);
		for (int i = 0; i < mListOptions.length; i++) {
			rcontent.add(mListOptions[i]);
		}
		String temp = match.getMatcha();
		temp = temp.trim();
		String[] mListItems = temp.split(";");
		lcontent = new ArrayList<String>(mListItems.length);
		for (int i = 0; i < mListItems.length; i++) {
			lcontent.add(mListItems[i]);
		}

		images = new ArrayList<Integer>();
		for (int i = 0; i < mListOptions.length; i++) {
			images.add(R.drawable.icmpmove);
		}
		String tempans = match.getAnswer();
		tempans = tempans.trim();
		String[] mListans = tempans.split(";");
		
		answers = new HashMap<String, String>();
		for (int i = 0; i < mListans.length; i++) {
			answers.put(mListItems[Integer.parseInt(mListans[i])- 1],mListOptions[i]);
		}

		allContents = new ArrayList();
		allContents.add(lcontent);
		allContents.add(rcontent);
		allContents.add(images);
		allContents.add(answers);
	}

	private DropListener mDropListener = new DropListener() {
		public void onDrop(int from, int to) {
			ListAdapter adapter = getListAdapter();
			if (adapter instanceof DragDropAdapter) {
				((DragDropAdapter) adapter).onDrop(from, to);
				getListView().invalidateViews();
			}
		}
	};

	private RemoveListener mRemoveListener = new RemoveListener() {
		public void onRemove(int which) {
			ListAdapter adapter = getListAdapter();
			if (adapter instanceof DragDropAdapter) {
				((DragDropAdapter) adapter).onRemove(which);
				getListView().invalidateViews();
			}
		}
	};

	private DragListener mDragListener = new DragListener() {
		int backgroundColor = Color.TRANSPARENT;// BLUE; //0xe0103010;
		int defaultBackgroundColor;

		public void onDrag(int x, int y, ListView listView) {
			// TODO Auto-generated method stub
		}

		public void onStartDrag(View itemView) {
			itemView.setVisibility(View.INVISIBLE);
			defaultBackgroundColor = itemView.getDrawingCacheBackgroundColor();
			itemView.setBackgroundColor(backgroundColor);
			ImageView iv = (ImageView) itemView.findViewById(R.id.ImageView01);
			if (iv != null)
				iv.setVisibility(View.INVISIBLE);
		}

		public void onStopDrag(View itemView) {
			itemView.setVisibility(View.VISIBLE);
			itemView.setBackgroundColor(defaultBackgroundColor);
			ImageView iv = (ImageView) itemView.findViewById(R.id.ImageView01);
			if (iv != null)
				iv.setVisibility(View.VISIBLE);
		}

	};
	
}